#include "auditcore.h"


#include <string.h>

#include <QtCore>


#include "sql/srdatabase.h"
#include "sql/oesupermanagersqldatabase.h"
#include "sql/oeloginsqldatabase.h"
#include "auditcryptogram.h"

OELoginSqlDatabase g_db;

#define SUPERVISOR_UESR ("SUPER_ADMIN_SR")

AUDITAPISHARED_EXPORT int apiRegisterAccount(const char * const _name,
                                             const char * const _user,
                                             const char * const _pswd,
                                             const char* const _issue1,
                                             const char* const _answer1,
                                             const char* const _issue2,
                                             const char* const _answer2) {
    QString name = QString::fromUtf8(_name);
    QString user = QString::fromUtf8(_user);
    QString pswd = QString::fromUtf8(_pswd);
    QString issue1 = QString::fromUtf8(_issue1);
    QString answer1 = QString::fromUtf8(_answer1);
    QString issue2 = QString::fromUtf8(_issue2);
    QString answer2 = QString::fromUtf8(_answer2);

    return OESuperManagerSqlDatabase::registerUser(
                user, pswd, name, issue1,answer1,issue2,answer2);
}

AUDITAPISHARED_EXPORT int apiResetAccount(const char * const _user,
                                          const char * const _pswd) {
    do {
        if (_user == Q_NULLPTR &&
                _pswd != Q_NULLPTR
            && 0 == strcmp(_pswd,"admin")) {
            // 备份db文件
            QString db_file;
            SRDatabase::DBPath(db_file);
            QFile file(db_file);
            if (file.exists()) {
                QFile::remove(db_file);
            }
            return 0;
        }
        else if (_user != Q_NULLPTR
            && _pswd != Q_NULLPTR) {
            // 验证
            if (1)
                break;
            else
                return -1;
        }
    } while(false);

    // 备份db文件
    QString db_file = OESuperManagerSqlDatabase::dbPath();
    QFile file(db_file);
    if (file.exists()) {
        QFile::rename(db_file, db_file
               + QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz"));
    }

    return 0;
}

AUDITAPISHARED_EXPORT int apiLoginAccount(const char * const _user,
                                          const char * const _pswd,
                                          char* const _username) {
    QString user = QString::fromUtf8(_user);
    QString pswd = QString::fromUtf8(_pswd);
    QString userName;
    int ret = g_db.login(user,pswd,userName);
    if (_username != Q_NULLPTR)
        memcpy(_username,userName.toUtf8().data(),userName.toUtf8().length());
    return ret;
}

AUDITAPISHARED_EXPORT int apiLogoutAccount(void) {
    int ret = g_db.logout();
    return ret;
}

AUDITAPISHARED_EXPORT int apiCheckRegistered(void) {
    if (!OESuperManagerSqlDatabase::isExist()) {
        return 1073;
    }
    if (0 != OESuperManagerSqlDatabase::checkDB()) {
        return 1073;
    }
    return 0;
}



AUDITAPISHARED_EXPORT int apiRegisterSupervisor(const char * const )
{
//    return OESuperManagerSqlDatabase::registerUser(SUPERVISOR_UESR, _pswd, "admin");
    return 0;
}

AUDITAPISHARED_EXPORT int apiModifyAccountName(const char * const _user,
                                               const char * const _username,
                                               const char * const _superPswd) {
    Q_UNUSED(_superPswd);
//    if (ret != 0) {
//        return ret;
//    }

    int ret = OESuperManagerSqlDatabase::modifyName(_user,"",_username);
    return ret;
}

AUDITAPISHARED_EXPORT int apiCheckSupervisorPswd(const char * const _pswd)
{
    return OESuperManagerSqlDatabase::checkPswd(SUPERVISOR_UESR, _pswd);
}

AUDITAPISHARED_EXPORT int apiModifyAccountPswd(const char * const _user,
                         const char * const _pswd,
                         char * const )
{
//    int ret = apiCheckSupervisorPswd(_superPswd);
//    if (ret != 0) {
//        return ret;
//    }

    // 修改密码
    int ret = OESuperManagerSqlDatabase::modifyPswd(_user,"",_pswd);
    return ret;
}

int apiCheckAccountInfo(const char * const _user,
                        const char * const _issue1,
                        const char * const _answer1,
                        const char * const _issue2,
                        const char * const _answer2)
{
    QString user, issue1, answer1, issue2, answer2;

    user = QString::fromUtf8(_user);
    issue1 = QString::fromUtf8(_issue1);
    answer1 = QString::fromUtf8(_answer1);
    issue2 = QString::fromUtf8(_issue2);
    answer2 = QString::fromUtf8(_answer2);

    int ret = OESuperManagerSqlDatabase::checkAccountInfo(user,issue1,answer1,issue2,answer2);
    return ret;
}

int apiGetAccountInfo(const char * const _user,
                      char* const _username,
                      char * const _issue1, char * const _issue2)
{
    QString username,issue1, issue2;
    int ret = OESuperManagerSqlDatabase::getAccountInfo(_user, username, issue1,issue2);
    if (username.isEmpty()) {
        return -1;
    }

    if (_username != Q_NULLPTR)
        memcpy(_username, username.toUtf8().data(), username.toUtf8().length());
    if (_issue1 != Q_NULLPTR)
        memcpy(_issue1, issue1.toUtf8().data(), issue1.toUtf8().length());
    if (_issue2 != Q_NULLPTR)
        memcpy(_issue2, issue2.toUtf8().data(), issue2.toUtf8().length());

    return ret;
}
